﻿using System;
using System.Collections.Generic;
using System.Text;

using Application = System.Windows.Forms.Application;

namespace Misc.Common.Exceptions
{
	class ReportPartOS : IReportPartSource
	{
		#region IReportPartSource Members

		public string GetReportPart(ExceptionDescription exd) 
		{
			StringBuilder sb = new StringBuilder();
			TimeSpan      ts = new TimeSpan(Environment.TickCount * (long)10000);

			sb.AppendFormat("Версия ОС: {0}\n",            Environment.OSVersion);
			sb.AppendFormat("Версия CLR: {0}\n",           Environment.Version);
			sb.AppendFormat("Системная директория: {0}\n", Environment.SystemDirectory);
			sb.AppendFormat("Имя пользователя: {0}\n",     Environment.UserName);
			sb.AppendFormat("Рабочее время: {0}\n",        ts);

			sb.Append(Environment.HasShutdownStarted ? "БЫЛА НАЧАТА ПЕРЕЗАГРУЗКА!\n" : null);

			return sb.ToString();
		}

		public string ReportPartSourceName { get { return "OS"; } }

		#endregion
	}

	class ReportPartApp : IReportPartSource
	{
		#region IReportPartSource Members

		public string GetReportPart(ExceptionDescription exd) 
		{
			StringBuilder sb = new StringBuilder();

			sb.AppendFormat("Исполняемый файл: {0}\n",   Application.ExecutablePath);
			sb.AppendFormat("Параметры: {0}\n",          Environment.CommandLine);
			sb.AppendFormat("Текущая директория: {0}\n", Environment.CurrentDirectory);
			sb.AppendFormat("открытых форм: {0}\n",      Application.OpenForms.Count);

			int i = 0;
			foreach (System.Windows.Forms.Form cur in Application.OpenForms) 
			{
				i++;
				sb.AppendFormat("  №{0} - {1} ({2}) {3}", i, cur.Text, cur.Name, cur.WindowState);
				
				if (cur.IsDisposed) sb.Append(" *(Disposed!)* ");
				if (cur.Modal)      sb.Append(" *Modal* ");
				if (cur.TopLevel)   sb.Append(" *Parent* ");

				sb.AppendLine();
			}
			return sb.ToString();
		}

		public string ReportPartSourceName { get { return "Application"; } }

		#endregion
	}
	/*class ReportPartMachine : IReportPartSource
	{



		#region IReportPartSource Members

		public string getReportPart(ExceptionDesc exd) {
			string s5 = Environment.ProcessorCount;
		}

		public string getReportPartSourceName() {
			return "Machine";
		}

		#endregion
	}*/


	/// <summary>
	/// 
	/// </summary>
	/// <remarks>
	/// BUG: IP адрес показывается только один и он - 127.0.0.1.
	/// </remarks>
	class ReportPartNet : IReportPartSource
	{
		#region IReportPartSource Members

		public string GetReportPart(ExceptionDescription exd) 
		{
			StringBuilder sb = new StringBuilder();

			sb.AppendFormat("NetBios имя: {0}\n", Environment.MachineName);
			sb.AppendFormat("DNS имя: {0}\n",     System.Net.Dns.GetHostName());
			sb.AppendFormat("Имя домена: {0}\n",  Environment.UserDomainName);
			sb.Append      ("IP адреса:\n");

			System.Net.IPHostEntry he = System.Net.Dns.GetHostEntry(System.Net.IPAddress.Loopback);

			for (int i = 0; i < he.AddressList.Length; i++) 
			{
				sb.AppendFormat(he.AddressList[i].ToString());

				if (i < he.AddressList.Length - 1) { sb.Append(", "); }
			}

			return sb.ToString();

		}

		public string ReportPartSourceName { get { return "Net"; } }

		#endregion
	}
}
